﻿using System.Collections.Generic;
using System.Web.Mvc;
using TSV.Models;
using TSV.Library.Common;
using System.Collections;
using TSV.Areas.Administration.Models;
using Pagination;
namespace TSV.Controllers
{
    public class HomeController : BaseViewController
    {
        #region Variable
        AdvertisingDao advertisingDao = new AdvertisingDao();
        PartnerDao partnerDao = new PartnerDao();
        private CategoryDao catDao = new CategoryDao();
        int defaultPageSize = Constants.DEFAULT_PAGE_SIZE;
        #endregion
        public ActionResult Index(int? page)
        {
              
            
            ViewData["HomeNewPartner"] = partnerDao.GetListNewPartner();
            List<sp_GetPromotionPartnerResult> listPromotionPartner = partnerDao.GetListPromotionPartner();
            //ViewData["HomePromotionPartner"] = listPromotionPartner;
            //ViewData["CategoryList"] = new CategoryDao().GetList();
           
            ViewData["HomeNews"] = new NewsDao().GetTopNew();
            int currentPageIndex = page.HasValue ? page.Value : 1;
            var list = listPromotionPartner.ToPagedList(currentPageIndex, defaultPageSize);
            if (Request.IsAjaxRequest())
                return PartialView("List", list);
            else
                return View(list);
        }
        [HttpPost]
        public JsonResult CreateEmail(string email)
        {
            JsonResult result = new JsonResult();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            EmailRegisterDao dao = new EmailRegisterDao();
            Message msg = null;
            if (CheckUtil.IsEmail(email))
            {
                if (dao.CheckEmailExits(email) == null)
                {
                    Email objEmail = new Email();
                    objEmail.Email1 = email;
                    msg = dao.Insert(objEmail);
                    if (msg.MsgType == MessageType.Info)
                    {
                        msg = new Message(MessageConstants.I0005, MessageType.Info);
                    }
                }
                else
                {
                    msg = new Message(MessageConstants.E0041, MessageType.Error);
                   
                }
            }
            else
            {
                msg = new Message(MessageConstants.E0004, MessageType.Error);
            }
            result.Data = msg;
            return result;
        }

        [HttpPost]
        public ActionResult SetFilter(FormCollection request)
        {
            JsonResult result = new JsonResult();

            Hashtable hash = new Hashtable();
            hash.Add(Constants.GLOBAL_FILTER_TEXT, request["txtGlobalSearch"] != Constants.GLOBAL_FILTER_TEXT_DEFAULT ? request["txtGlobalSearch"] : string.Empty);
            hash.Add(Constants.GLOBAL_FILTER_CATEGORY, request["GlobalCategory"]);
            hash.Add(Constants.GLOBAL_FILTER_PROVINCE, request["MasterProvinceList"]);
            hash.Add(Constants.GLOBAL_FILTER_DISTRICT, request["MasterDistrictList"]);
            Session[SessionKey.GLOBALSEARCHDATA] = hash;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            Message msg = new Message(MessageConstants.I0005, MessageType.Info);
            result.Data = msg;

            return result;

            //return Redirect("/timkiem.html");
        }

        public ActionResult SelectPartnerList(string province, string category)
        {
            Hashtable hash = new Hashtable();
            hash.Add(Constants.GLOBAL_FILTER_TEXT, string.Empty);
            hash.Add(Constants.GLOBAL_FILTER_CATEGORY, string.Empty);
            hash.Add(Constants.GLOBAL_FILTER_PROVINCE, province);
            hash.Add(Constants.GLOBAL_FILTER_DISTRICT, string.Empty);
            Session[SessionKey.GLOBALSEARCHDATA] = hash;
            return Redirect("/timkiem.html");
        }

        public ActionResult SelectPartnerListByCatId(string id)
        {
            string selected_Province = string.Empty;
            if (Session[SessionKey.GLOBALSEARCHDATA] != null)
            {
                Hashtable hashSearching = (Hashtable)Session[SessionKey.GLOBALSEARCHDATA];
                string province_value = (string)hashSearching[Constants.GLOBAL_FILTER_PROVINCE];
                if (!string.IsNullOrEmpty(province_value) && CheckUtil.IsPositiveNumber(province_value))
                {
                    selected_Province = province_value;

                }               
            }
            Hashtable hash = new Hashtable();
            hash.Add(Constants.GLOBAL_FILTER_TEXT, string.Empty);
            hash.Add(Constants.GLOBAL_FILTER_CATEGORY, id);
            hash.Add(Constants.GLOBAL_FILTER_PROVINCE, selected_Province);
            hash.Add(Constants.GLOBAL_FILTER_DISTRICT, string.Empty);
            Session[SessionKey.GLOBALSEARCHDATA] = hash;
            return Redirect("/timkiem.html");
        }

        public ActionResult GlobalSearch(int? page)
        {
            //ViewData["AdvertisingLeft"] = new AdvertisingDao().GetAdvertisingLeft();
            string text = string.Empty;
            int category = 0;
            int provinceid = 0;
            int districtid = 0;
            if (Session[SessionKey.GLOBALSEARCHDATA] != null)
            {
                Hashtable hash = (Hashtable)Session[SessionKey.GLOBALSEARCHDATA];
                text = (string)hash[Constants.GLOBAL_FILTER_TEXT];
                string value = (string)hash[Constants.GLOBAL_FILTER_CATEGORY];
                if (!string.IsNullOrEmpty(value) && CheckUtil.IsPositiveNumber(value))
                {
                    category = int.Parse(value);
                }
                string province_value = (string)hash[Constants.GLOBAL_FILTER_PROVINCE];
                if (!string.IsNullOrEmpty(province_value) && CheckUtil.IsPositiveNumber(province_value))
                {
                    provinceid = int.Parse(province_value);
                }
                string district_value = (string)hash[Constants.GLOBAL_FILTER_DISTRICT];
                if (!string.IsNullOrEmpty(district_value) && CheckUtil.IsPositiveNumber(district_value))
                {
                    districtid = int.Parse(district_value);
                }
            }
            List<sp_GetGlobalSearchResult> partnerList = new PartnerDao().GetListByGlobalSearch(text, category, provinceid,districtid);
            int currentPageIndex = page.HasValue ? page.Value : 1;
            var list = partnerList.ToPagedList(currentPageIndex, defaultPageSize);
            if (Request.IsAjaxRequest())
                return PartialView("List_SearchResult", list);
            else
                return View(list);
        }
    }
}
